package com.zhonghui.scm.service.impl;

import com.zhonghui.common.utils.DateUtils;
import com.zhonghui.common.utils.SecurityUtils;
import com.zhonghui.scm.domain.ScmPurchaseApply;
import com.zhonghui.scm.domain.ScmPurchaseContract;
import com.zhonghui.scm.domain.ScmPurchaseContractItem;
import com.zhonghui.scm.domain.ScmPurchasePlan;
import com.zhonghui.scm.mapper.ScmPurchaseApplyMapper;
import com.zhonghui.scm.mapper.ScmPurchaseContractMapper;
import com.zhonghui.scm.service.IScmPurchaseContractService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 采购合同Service业务层处理
 *
 * @author zhonghui
 * @date 2022-05-26
 */
@Service
public class ScmPurchaseContractServiceImpl implements IScmPurchaseContractService
{
    @Autowired
    private ScmPurchaseContractMapper scmPurchaseContractMapper;

    @Autowired
    private ScmPurchaseApplyMapper scmPurchaseApplyMapper;

    /**
     * 查询采购合同
     *
     * @param id 采购合同主键
     * @return 采购合同
     */
    @Override
    public ScmPurchaseContract getById(Long id)
    {
        return scmPurchaseContractMapper.getById(id);
    }

    /**
     * 查询采购合同列表
     *
     * @param scmPurchaseContract 采购合同
     * @return 采购合同
     */
    @Override
    public List<ScmPurchaseContract> getList(ScmPurchaseContract scmPurchaseContract)
    {
        return scmPurchaseContractMapper.getList(scmPurchaseContract);
    }

    /**
     * 新增采购合同
     *
     * @param scmPurchaseContract 采购合同
     * @return 结果
     */
    @Override
    public int add(ScmPurchaseContract scmPurchaseContract)
    {
        scmPurchaseContract.setContractNo(generateNumber("CGHT"));
        scmPurchaseContract.setApplicantId(SecurityUtils.getUserId());
        scmPurchaseContract.setApplicantDate(DateUtils.getNowDate());
        int row = scmPurchaseContractMapper.add(scmPurchaseContract);
        insertItem(scmPurchaseContract);
        return row;
    }

    /**
     * 修改采购合同
     *
     * @param scmPurchaseContract 采购合同
     * @return 结果
     */
    @Override
    public int edit(ScmPurchaseContract scmPurchaseContract)
    {
        scmPurchaseContractMapper.delItem(scmPurchaseContract.getId());
        insertItem(scmPurchaseContract);
        return scmPurchaseContractMapper.edit(scmPurchaseContract);
    }

    /**
     * 批量删除采购合同
     *
     * @param ids 需要删除的采购合同主键
     * @return 结果
     */
    @Override
    public int del(Long[] ids)
    {
        for (Long id : ids) {
            scmPurchaseContractMapper.delItem(id);
        }
        return scmPurchaseContractMapper.del(ids);
    }


    public void insertItem(ScmPurchaseContract scmPurchaseContract){
        List<ScmPurchaseContractItem> scmPurchaseContractItemList = scmPurchaseContract.getScmPurchaseContractItemList();

        if (!scmPurchaseContractItemList.isEmpty()) {
            for (ScmPurchaseContractItem scmPurchaseContractItem : scmPurchaseContractItemList) {
                scmPurchaseContractItem.setPurchasingContractId(scmPurchaseContract.getId());
            }

            scmPurchaseContractMapper.addItem(scmPurchaseContractItemList);
        }
    }

    public String generateNumber(String number){
        Date date = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        return number+format.format(date);
    }
}
